package com.tlgen.caffeine.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/*
 *@title DateUtils
 *@description
 *@author ta151486
 *@version 1.0
 *@create 2023/12/15 10:45
 */
public class DateUtils {

    public static String LocalDateTimeToString(LocalDateTime localDateTime) {
        Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return DateToString(from);
    }

    public static String DateToString(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }


    public static LocalDateTime DateStringToLocalDateTime(String dateString) {
        Date date = StringToDate(dateString);
        return DateToLocalDateTime(date);
    }

    private static LocalDateTime DateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    private static Date StringToDate(String date) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
